package com.yntsoft.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;

import com.github.pagehelper.PageInfo;
import com.yntsoft.entity.Role;
import com.yntsoft.entity.ShiroUser;
import com.yntsoft.entity.User;
import com.yntsoft.service.RoleService;
import com.yntsoft.service.UserService;
import com.yntsoft.util.Constant;
import com.yntsoft.util.ResponCode;

/**
 * 用户管理控制层
 * @author Tony
 *
 */
@Controller
@RequestMapping("user")
public class UserManagerController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	
	/**
	 * 用户列表分页查询
	 * @param request
	 * @param model 
	 * @return
	 */
	@RequestMapping(value="userPage")
	@RequiresPermissions("user:queryList")
	public String userPage(HttpServletRequest request,Model model){
		Map<String, Object> searchParam = Servlets.getParametersStartingWith(request,"search_");
		PageInfo<User> users = userService.queryUserList(searchParam,Constant.pageSize);
		model.addAttribute("users", users);
		return "user/userPage";
	}
	
	
	/**
	 * 添加或者修改用户信息
	 * @param request
	 * @param user
	 * @param attributes
	 * @return  
	 */
	@RequestMapping(value="addUser" ,method=RequestMethod.POST)
	@RequiresPermissions("user:addUser")
	public String saveUser(HttpServletRequest request,User user,RedirectAttributes redirectAttributes){
		user.setIsLock(User.IS_LOCK_2);
		if(user.getId() != null){//修改
			Integer rst = userService.saveUser(user);
			if(rst!=null){
				redirectAttributes.addFlashAttribute("message", "修改成功");
			}else{
				redirectAttributes.addFlashAttribute("message", "修改失败");
			}
		}else{//添加
			Integer rst = userService.saveUser(user);
			if(rst!=null){
				redirectAttributes.addFlashAttribute("message", "添加成功");
			}else{
				redirectAttributes.addFlashAttribute("message", "添加失败");
			}
		} 
		return "redirect:/user/userPage.do";
	}
	
	/**
	 * 获取用户信息
	 * @param request
	 * @param user
	 * @param attributes
	 * @return   
	 */
	@RequestMapping("getUserInfo")
	@ResponseBody  
	@RequiresPermissions("user:getUserInfo")
	public Map<String, Object> getUserInfo(HttpServletRequest request,Integer userId,Model model){
		Map<String, Object> map = new HashMap<String,Object>();
		if (userId != null ) {
			User user = userService.getUserById(userId);
			if(user != null ){
				map.put("code", 200);
				map.put("user", user);
			}else{
				map.put("code", 400);
			}
		}else{
			map.put("code", 400);
		}
		return map;
	}


	/**
	 * 获取用户信息
	 * @param request
	 * @param user
	 * @param attributes
	 * @return  
	 */
	@RequestMapping("userDel")
	@RequiresPermissions("user:userDel")
	public String userDel(HttpServletRequest request,RedirectAttributes redirectAttributes,String ids){
		if (ids != null && ids!="") {
			Integer rst = userService.userDel(ids);
			if(rst != null && rst != 0){
				redirectAttributes.addFlashAttribute("message", "删除成功");
			}else{
				redirectAttributes.addFlashAttribute("message", "删除失败");
			}
		}else{
			redirectAttributes.addFlashAttribute("message", "删除失败");
		}
		return "redirect:/user/userPage.do";
	}
	
	
	/**
	 * 锁定账户
	 * @param request
	 * @param redirectAttributes
	 * @param ids 要锁订的用户id
	 * @param type 类型  解锁  锁定 
	 * @return
	 */
	@RequestMapping("lockUser") 
	@RequiresPermissions("user:lockUser")
	public String lockUser(HttpServletRequest request,RedirectAttributes redirectAttributes,String ids,String type){
		if (ids != null && ids!="") {
			Integer rst = userService.userDel(ids);
			if(rst != null && rst != 0){
				redirectAttributes.addFlashAttribute("message", "删除成功");
			}else{
				redirectAttributes.addFlashAttribute("message", "删除失败");
			}
		}else{
			redirectAttributes.addFlashAttribute("message", "删除失败");
		}
		return "redirect:/user/userPage.do";
	}
	
	@RequestMapping("getUserRole") 
	@RequiresPermissions("user:getUserRole")
	@ResponseBody
	public Map<String, Object> getUserRole(HttpServletRequest request,Integer userId){
		Map<String, Object> map = new HashMap<String,Object>();
		List<Role>  roles = roleService.getUserRole(userId);
		map.put("roles", roles);
		return map;
	}
	
	
	@RequestMapping("setUserRole") 
	@RequiresPermissions("user:setUserRole")
	@ResponseBody
	public Map<String, Object> setUserRole(HttpServletRequest request,String realRoleId){
		Map<String, Object> map = new HashMap<String,Object>();
		Subject subject = SecurityUtils.getSubject();
		ShiroUser shiroUser = (ShiroUser) subject.getPrincipal();
		Integer rst =  roleService.setUserRole(realRoleId,shiroUser.getUserId());
		if(rst != 0){
			map.put("code",ResponCode.code_success);
		}else{
			map.put("code",ResponCode.code_fail);
		}
		return map;
	}
}
